package com.epam.klymanov.pathProcessing;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import com.epam.klymanov.tools.PathUtility;
import com.epam.klymanov.tools.PollutionMatrix;
import com.epam.klymanov.tools.Polygons;
import com.epam.klymanov.tools.TypeOfShit;
import com.google.gson.Gson;
import com.greenway.tools.Location;

public class ShitCounter {
	
	public static ArrayList<PolutionDose> ShitDose(List<Location> way){
		
	PollutionMatrix pm;
	ArrayList<TypeOfShit> diffShit = new ArrayList<TypeOfShit>();
	
	diffShit.add(new TypeOfShit(0.2, "#00008B", PathUtility
			.getProjectRoot() + "PollutionPolygons.json"));
	diffShit.add(new TypeOfShit(0.1, "#EEAD0E", PathUtility
			.getProjectRoot() + "PollutionPolygonsCO2.json"));

	ArrayList<PolutionDose> doses = new ArrayList<PolutionDose>();
	for (TypeOfShit shit : diffShit) {
		
		
		try {
			Gson gson = new Gson();
			BufferedReader bufferedReader = new BufferedReader(
					new FileReader(shit.getPath()));
			Polygons polygons = gson.fromJson(bufferedReader,
					Polygons.class);
			
			pm = polygons.getPollutionMatrix();
			
			int curentWayDose = 0;
			for(int i =0 ; i< way.size(); i++){
				
				curentWayDose += pm.getMatrix()[pm.getX(way.get(i).getLng())][pm.getY(way.get(i).getLat())];
			}
		
			String name;
			if(shit.getColor().equals("#00008B")){name = "Lead pollution";}
			else if(shit.getColor().equals("#EEAD0E")){name = "CO2 pollution";}
			else{name = "Other pollution";}
			
			doses.add(new PolutionDose(curentWayDose, name) );
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	return doses;	
	}
}
